﻿
using System;
using System.Data;
using System.Collections.Generic;
using BYFW.SFL;

namespace LDSP.BRL
{
    public partial class Catalog : BYFW.BOL.BaseEntity
    {
        public Catalog()
        {
            m_tableName = "t_catalog";
            m_hasName = true;
            m_hasComment = true;
            m_tblCacheManager.IsEnable = true;
            m_objCacheManager.Size = 1000;
        }

        protected int m_enterpriseID = 0;
        protected int m_parentID = 0;
        protected string m_fullPath = string.Empty;
        protected Catalog m_parent = null;
        protected Enterprise m_enterprise = null;

        /// <summary>
        /// 关联的企业ID，0为默认
        /// </summary>
        public int EnterpriseID
        {
            get { return m_enterpriseID; }
            set { m_enterpriseID = value; }
        }
        /// <summary>
        /// 父类别ID
        /// </summary>
        public int ParentID
        {
            get { return m_parentID; }
            set { m_parentID = value; }
        }
        /// <summary>
        /// 表达层次的全路径：-id-格式
        /// </summary>
        public string FullPath
        {
            get { return m_fullPath; }
        }
        /// <summary>
        /// 父目录
        /// </summary>
        public Catalog Parent
        {
            get
            {
                if (m_parent == null)
                {
                    m_parent = new Catalog();
                    if (m_parentID != 0)
                    {
                        m_parent.GetProperties(m_parentID);
                    }
                }
                return m_parent;
            }
        }

        public Enterprise Enterprise
        {
            get
            {
                if (m_enterprise == null)
                {
                    m_enterprise = new Enterprise();
                    if (m_enterpriseID != 0)
                    {
                        m_enterprise.GetProperties(m_enterpriseID);
                    }
                }
                return m_enterprise;
            }
        }

        protected override void FillContainer()
        {
            base.FillContainer();
            m_container.Add("enterpriseID", m_enterpriseID);
            m_container.Add("parentID", m_parentID);
            m_container.Add("fullPath", m_fullPath);
        }

        public override bool GetProperties(System.Data.DataRow record)
        {
            if (!base.GetProperties(record))
            {
                return false;
            }
            m_enterpriseID = this.GetIntValue(record, "enterpriseID");
            m_parentID = this.GetIntValue(record, "parentID");
            m_fullPath = this.GetStringValue(record, "fullPath");
            return true;
        }
        public override int Add(string transID)
        {
            base.Add(transID);
            this.CreateFullPath();
            this.Update(transID);
            return this.ID;
        }

        public List<Catalog> GetListByParentID(int parentID)
        {
            return this.GetSpecialList<Catalog>(null, "parentID", parentID);
        }

        public Catalog GetCatalogByID(int id)
        {
            Catalog res = new Catalog();
            res.GetProperties(id);
            return res;
        }

        public List<Catalog> GetAllCatalogs()
        {
            return this.GetSpecialList<Catalog>(null, "");
        }

        public List<Catalog> GetListRootByDefault()
        {
            return GetListRootByEnterpriseID(0);
        }
        public List<Catalog> GetListRootByEnterpriseID(int enterpriseID)
        {
            string cond = string.Format("where {0}={1} and {2}={3}", DB.GetCol("parentID"), DB.GetVal(0), DB.GetCol("enterpriseID"), DB.GetVal(enterpriseID));
            return this.GetSpecialList<Catalog>(null, cond);
        }

        public void DeleteByParent(string transID, int parentID)
        {
            List<Catalog> children = this.GetListByParentID(parentID);
            for (int i = 0; i < children.Count; i++)
            {
                children[i].Delete(transID);
            }
        }
        public void DeleteByEnterprise(string transID, int enterpriseID)
        {
            List<Catalog> catalogList = new Catalog().GetListRootByEnterpriseID(enterpriseID);
            for (int i = 0; i < catalogList.Count; i++)
            {
                catalogList[i].Delete(transID);
            }
            //防止栏目脏数据
            catalogList = this.GetSpecialList<Catalog>(transID, "enterpriseID", enterpriseID);
            for (int i = 0; i < catalogList.Count; i++)
            {
                catalogList[i].Delete(transID);
            }
        }

        /// <summary>
        /// 创建表达层次的全路径，必须在对象已添加到数据库后使用此方法
        /// </summary>
        /// <returns></returns>
        protected void CreateFullPath()
        {
            m_fullPath = (Parent.ID == 0 ? "-" : Parent.FullPath) + this.ID + "-";
        }

        public override int Delete(string transID)
        {
            //删除子栏目
            this.DeleteByParent(transID, m_id);
            //删除本栏目下的文档
            new Codification().DeleteByCatalog(transID, m_id);
            //删除授权关系
            new R_EnterpriseCatalog().DeleteByCatalog(transID, m_id);
            //删除管理员栏目权限
            new R_SystemUserCatalog().DeleteByCatalog(transID, m_id);
            return base.Delete(transID);
        }
    }
}
